package com.sdy.healthcode.h5.task;

import com.sdy.healthcode.biz.model.ExpertsInformation;
import com.sdy.healthcode.biz.service.ExpertsInformationService;
import com.sdy.mvc.annotation.TaskSync;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 医生状态任务
 */
@Component
@Slf4j
public class ExpertsStateTask {
    @Autowired
    private ExpertsInformationService expertsInformationService;

    /**
     * 每周一恢复临时停诊医生
     */
    @Scheduled(cron = "20 0 0 * * 1")
//    @Scheduled(cron = "0/60 * * * * ?")
    @TaskSync
    public void daily() {
        log.info("开始恢复临时停诊医生");
        List<ExpertsInformation> expertsInformations = expertsInformationService.lambdaQuery()
                .eq(ExpertsInformation::getSuspendService, 4)
                .list();
        if (!expertsInformations.isEmpty()) {
            expertsInformationService.lambdaUpdate()
                    .inOrThrow(ExpertsInformation::getId,
                            expertsInformations.stream().map(ExpertsInformation::getId).collect(Collectors.toSet()))
                    .set(ExpertsInformation::getSuspendService, 1)
                    .set(ExpertsInformation::getReplaceDoctor, null)
                    .update();
        }
        log.info("恢复临时停诊医生结束，恢复医生：[{}]，ID：[{}]",
                expertsInformations.stream().map(ExpertsInformation::getName).collect(Collectors.joining(",")),
                expertsInformations.stream().map(ExpertsInformation::getId).map(Object::toString).collect(Collectors.joining(",")));
    }
}
